package 力扣;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: a
 * Date: 2022-09-11
 * Time: 20:13
 */
public class permutations {
    private static List<List<Integer>> res;
    private static boolean[] vic;

    public static List<List<Integer>> permute(int[] nums) {
        int len = nums.length;
        res = new ArrayList<>();
        vic = new boolean[len];
        List<Integer> list = new ArrayList<>();
        permuteHelper(nums, 0, len, list);
        return res;
    }

    private static void permuteHelper(int[] nums, int start, int end, List<Integer> list){
        if(start == end){
            res.add(new ArrayList<>(list));
            return;
        }
        for(int i = 0; i < end; i++){
            if(!vic[i]){
                vic[i] = true;
                list.add(nums[i]);
                permuteHelper(nums, start + 1, end, list);
                list.remove(list.size() - 1);
                vic[i] = false;
            }
        }
    }

    public static void main(String[] args) {
        int[] nums = {1, 2, 3};
        permute(nums);
    }
}
